package com.cpldynamics.core.guicomponents.buttons;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolTip;

import com.cpldynamics.core.utils.GUIConstants;
import com.cpldynamics.core.utils.SkeletonObjects;

public class CDarkButton extends JButton implements SkeletonObjects {

	private static final long serialVersionUID = 6533756109933829314L;

    private Image buttonHighlight;
    private Image buttonArmed;


    private float shadowOffsetX;
    private float shadowOffsetY;
    private Color colorDeSombra=new Color(0,0,0);
    private int direccionDeSombra=60;
    private int distanciaDeSombra=1;
    private boolean foco=false;

    public CDarkButton(Icon icon) {
    	super(icon);
	}
    
    public CDarkButton() {
    	 setOpaque(false);
         setContentAreaFilled(false);
         setBorderPainted(false);
         setFocusPainted(false);
         setFont(new Font("Calibri", Font.BOLD, 14));
         setForeground(new Color(255,255,255));
         
         
         
         buttonHighlight = loadImage(GUIConstants.RESOURCE_PATH.concat(GUIConstants.ICON_BLACK_DEFAULT));
         buttonArmed = loadImage(GUIConstants.RESOURCE_PATH.concat(GUIConstants.ICON_BLACK_PRESSED));
         
         
         
         addFocusListener(new FocusListener() {

             public void focusGained(FocusEvent e) {
                 foco=true;
             }

             public void focusLost(FocusEvent e) {
                 foco=false;
             }
         });	
	}
	@Override
	public void initListeners() {
		
	}

	@Override
	public void initObjects() {
	
	}

	@Override
	public void initRenders() {
	
	}
	/*
	@Override
    public JToolTip createToolTip() {
        ToolTipRound tip = new ToolTipRound();
        tip.setComponent(this);
        return tip;
    }*/

    private static Image loadImage(String fileName) {
        try {
            return ImageIO.read(CDarkButton.class.getResource(fileName));
        } catch (IOException ex) {
            return null;
        }
    }
  

    @Override
    protected void paintComponent(Graphics g) {
        computeShadow();
        Graphics2D g2 = (Graphics2D) g;
        Paint oldPaint = g2.getPaint();

        ButtonModel modelo = getModel();
        Rectangle2D rect = new Rectangle2D.Double(0, 0, getWidth(), getHeight() / 2.0);
        
        if(modelo.isArmed() || modelo.isPressed()){
        	
        	//g2.setPaint(getBackground().darker());
        	g2.drawRect(0, 0, getWidth(), getHeight());
        	
        	g2.drawImage(buttonArmed,
                    0,0,
                    getWidth(), getHeight(), null);
        }
            
        else
            g2.setPaint(getBackground());
       
        g2.fill(rect);

        rect = new Rectangle2D.Double(0, (getHeight() / 2.0) - 1.0, getWidth(), getHeight());
        if(modelo.isArmed() || modelo.isPressed()){
        	
        	            
            
              g2.setPaint(getBackground().darker().ORANGE);
        }
            
        else
            g2.setPaint(getBackground().darker());
        
        g2.fill(rect);

        if(modelo.isRollover() || foco){
            g2.drawRect(0, 0, getWidth(), getHeight());
            g2.drawImage(buttonHighlight,
                    0,0,
                    getWidth(), getHeight(), null);
        }


        FontMetrics fm = getFontMetrics(getFont());
        TextLayout layout = new TextLayout(getText(),
                getFont(),
                g2.getFontRenderContext());
        Rectangle2D bounds = layout.getBounds();
        
        int x = (int) (getWidth() -  bounds.getWidth()) / 2;
        //x -= 2;
        int y = (getHeight() - fm.getMaxAscent() - fm.getMaxDescent()) / 2;
        y += fm.getAscent() - 1;
        
        if (modelo.isArmed()) {
            x += 1;
            y += 1;
        }
        
        g2.setColor(colorDeSombra);
        layout.draw(g2,
                x + (int) Math.ceil(shadowOffsetX),
                y + (int) Math.ceil(shadowOffsetY));
        if(isEnabled())
            g2.setColor(getForeground());
        else
            g2.setColor(getForeground().darker());

        layout.draw(g2, x, y);
        g2.setPaint(oldPaint);

    }

    private void computeShadow() {
        double rads = Math.toRadians(direccionDeSombra);
        shadowOffsetX = (float) Math.cos(rads) * distanciaDeSombra;
        shadowOffsetY = (float) Math.sin(rads) * distanciaDeSombra;
    }

   
    public Color getColorDeSombra() {
        return colorDeSombra;
    }

    public void setColorDeSombra(Color colorDeSombra) {
        this.colorDeSombra = colorDeSombra;
    }

    public int getDireccionDeSombra() {
        return direccionDeSombra;
    }

    public void setDireccionDeSombra(int direccionDeSombra) {
        this.direccionDeSombra = direccionDeSombra;
    }

    public int getDistanciaDeSombra() {
        return distanciaDeSombra;
    }

    public void setDistanciaDeSombra(int distanciaDeSombra) {
        this.distanciaDeSombra = distanciaDeSombra;
    }	

}
